package com.yyydjk.library;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;

import java.util.List;

/**
 * Created by xujl on 2018/2/7.
 * 下拉窗处理代理
 */

public class MenuDropWindowDelegate {
    /**
     * 弹出菜单父布局
     */
    private FrameLayout dropViewParent;
    /**
     * 弹出菜单的所有控件
     */
    private List<View> dropViews;
    private float menuHeightPercent = 0.5f;


    public void setDropViews (List<View> dropViews) {
        this.dropViews = dropViews;
    }

    public FrameLayout getDropViewParent () {
        return dropViewParent;
    }

    public void initDropViewParent (Context context) {
        dropViewParent = new FrameLayout(context);
        dropViewParent.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                (int) (DeviceUtils.getScreenSize(context).y * menuHeightPercent)));
        dropViewParent.setVisibility(View.GONE);
    }

    public float getMenuHeightPercent () {
        return menuHeightPercent;
    }

    public void setMenuHeightPercent (float menuHeighPercent) {
        this.menuHeightPercent = menuHeighPercent;
    }

    public void addDropViews (Context context) {
        for (View dropView : dropViews) {
            if (dropView == null) {
                View view = new View(context);
                view.setVisibility(View.GONE);
                dropViewParent.addView(view);
                continue;
            }
            dropViewParent.addView(dropView);
        }
    }

    public void closeMenu () {
        dropViewParent.setVisibility(View.GONE);
        dropViewParent.setAnimation(AnimationUtils.loadAnimation(dropViewParent.getContext(), R.anim.dd_menu_out));
    }

    public boolean showDrop (int position) {
        boolean isShow = true;
        for (int i = 0; i < dropViews.size(); i++) {
            final View view = dropViews.get(i);
            if (view == null) {
                isShow = !(i == position);
                continue;
            }
            view.setVisibility(i == position ? View.VISIBLE : View.GONE);
        }
        dropViewParent.setVisibility(isShow ? View.VISIBLE : View.GONE);
        return isShow;
    }

}
